package com.mastercard.assets.web.struts.actions;

import com.mastercard.assets.MasterCardException;
import com.mastercard.assets.jpa.TblCampania;
import com.mastercard.assets.jpa.TblGanador;
import com.mastercard.assets.jpa.TblPaquete;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.util.to.UserLogin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class VerGanadores 
        extends
		Action
{
    
    public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
    {
        
        HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		String cveCampania = request.getParameter( "cveCampania" ) ;
                String cveAsset = request.getParameter( "cveAsset" ) ;                
                int cvAsset = Integer.getInteger( cveAsset ) ;                
                TblCampania campania = new TblCampania( ) ;
                List< TblGanador > winners = new ArrayList< TblGanador >( ) ;
                
                int cve = Integer.parseInt( cveCampania ) ;
        try {
            campania = MasterCardDAO.getCampania( cve ) ;
        } catch (MasterCardException ex) {
            MasterCardDAO.closeEntityManager( ) ;
            Logger.getLogger(VerGanadores.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        List< TblPaquete > paquetes = campania.getTblPaquetes( ) ;
        
        for ( TblPaquete p : paquetes )
        {
            
            if( p.getTblAsset( ).getCveAsset( ) == cvAsset )
            {
                
                List< TblGanador > win = p.getTblGanador( ) ;
                
                for ( TblGanador w : win )
                {
                    winners.add( w ) ;
                }
                 
            }
            
        }
        
        // en caso de que se cumpla, guarda el WorkArea
			user.setNextWorkArea( "/WEB-INF/pages/WorkArea/RoleNoValido.jsp" ) ;
        
        request.setAttribute( "Win", winners );
        request.setAttribute( "cveCampania", cve );
        
        MasterCardDAO.closeEntityManager( ) ;
        return mapping.findForward( "success" ) ;
        
    }
    
}
